<?php

class Managecomment extends CI_Controller {

    function __construct() {
        parent::__construct();
        $id = $this->session->userdata('id');
        $adminname = $this->session->userdata('adminname');
        $hash = $this->session->userdata('hash');
        if (!empty($hash) && $hash == md5(SECRETCODE . $id . $adminname . SECRETCODE)) {
            $this->admin['name'] = $adminname;
            $this->admin['id'] = $id;
            $this->admin['hash'] = $hash;
        } else {
            //die($id . ' ' . $adminname);
            redirect(base_url() . 'admin/login');
        }
        $this->load->model('comment_m');
    }
    
    function addComment() {
        if ($this->admin['name'] == '' || $this->admin['id'] == '' || $this->admin['hash'] != md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            redirect(base_url() . 'admin/login');
        }
        $this->load->model('comment_m');
        $tmp = '';
        if ($this->input->post('add') == "Add") {
            $updata['name'] = $this->input->post('name');
            $updata['date_created'] = time();
            $updata['publish'] = $this->input->post('publish');
            $updata['language'] = $this->input->post('language');
            $updata['email'] = $this->input->post('email');
            $updata['content'] = $this->input->post('content'); 
            $updata['quote'] = $this->input->post('quote');
            $config['upload_path'] = './uploads/avatars';         
            $config['allowed_types'] = 'png|jpg|jpeg|gif';
            $config['max_size'] = 0;
            $this->load->library('upload', $config);

            if ($this->upload->do_upload('avatar')) {
                $tmp2 = $this->upload->data('avatar');
                $updata['avatar'] = 'uploads/avatars/' . $tmp2['file_name'];
            } else {
                //die('wtf');
                $updata['avatar'] = 'uploads/avatars/noavatar.gif';
            }
                $result = $this->comment_m->add($updata);
                if ($result) {
                    ?>
                    <script type="text/javascript">
                        alert("Add comment successfully");
                    </script>
                    <?php
                } else {
                    ?>
                    <script type="text/javascript">
                        alert("Upload failed");
                    </script>
                    <?php
               }
            
        } else {
            $this->load->view('admin/common/header');
            $this->load->view('admin/comment_add');
            $this->load->view('admin/common/footer');
        }
    }

    function index() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            /*   $this->load->library('pagination');
              $offset = $this->uri->segment(3);
              $limit = 12;
              $config['base_url'] = site_url('admin/managecomment');
              $config['uri_segment'] = 3;
              $config['total_rows'] = $this->db->count_all('comment');
              $config['per_page'] = $limit;
              $this->pagination->initialize($config);
              $data['paginator'] = $this->pagination->create_links(); */

            $data['comment'] = $this->comment_m->get();

            $this->load->view('admin/common/header');
            $this->load->view('admin/comment', $data);
            $this->load->view('admin/common/footer');
        } else {
            redirect(base_url() . 'admin/login');
        }
    }

    function editComment($id) {
        if ($this->admin['name'] == '' || $this->admin['id'] == '' || $this->admin['hash'] != md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            redirect(base_url() . 'admin/login');
        }
        $this->load->model('comment_m');
        $tmp = '';
        if ($this->input->post('edit') == "Edit") {
            $updata['name'] = $this->input->post('name');
            $updata['publish'] = $this->input->post('publish');
            $updata['email'] = $this->input->post('email');
            $updata['language'] = $this->input->post('language');
            $updata['content'] = $this->input->post('content');
            $updata['quote'] = $this->input->post('quote');            
            $config['allowed_types'] = 'png|jpg|jpeg|gif';
            $config['max_size'] = 0;
            $config['upload_path'] = './uploads/avatars';
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('avatar')) {
                $tmp2 = $this->upload->data('avatar');
                $updata['avatar'] = 'uploads/avatars/' . $tmp2['file_name'];
            }
            //else {die('wtf');   }
                $result = $this->comment_m->update($id,$updata);
                if ($result) {
                       redirect(base_url() . 'admin/managecomment');
                } else {
                    ?>
                    <script type="text/javascript">
                        alert("Upload failed");
                    </script>
                    <?php
               }
            
        } else {
            $data['comment'] = $this->comment_m->get($id);
            $this->load->view('admin/common/header');
            $this->load->view('admin/comment_edit',$data);
            $this->load->view('admin/common/footer');
        }
    }
    
    function update($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!is_numeric($id) || $id < 0)
                redirect(base_url() . 'admin/managecomment');

            if (isset($_GET['publish'])) {
                $data['publish'] = $this->input->get('publish');
                if ($data['publish'] == '')
                    $data['publish'] = 0;
                //die(print_r($data));
                if ($this->comment_m->update($id, $data)) {
                    //$this->_upload_files($id);
                    redirect(base_url() . 'admin/managecomment');
                }
                else
                    die('Lỗi cập nhật comment');
            }else {
                //die('xxx');
                $data['comments'] = $this->comment_m->get();
                $this->load->view('admin/common/header');
                $this->load->view('admin/comment_edit', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function delete($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!is_numeric($id))
                redirect(base_url() . 'admin/managecomment');
            if ($this->comment_m->delete($id)) {
                redirect(base_url() . 'admin/managecomment');
            }
            else
                die('Lá»—i xÃ³a danh má»¥c');
        } else
            redirect(base_url() . 'admin/login');
    }

    function delete_comment() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $ids = $this->input->post('selected');
            if (is_array($ids)) {
                if ($this->comment_m->delete($ids))
                    redirect(base_url() . 'admin/managecomment');
                else
                    die('Lá»—i xÃ³a danh má»¥c');
            }else {
                redirect(base_url() . 'admin/managecomment');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

}